/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifndef __CONNMAN_IPCONFIG_H
#define __CONNMAN_IPCONFIG_H

#ifdef __cplusplus
extern "C" {
#endif

#include <connman/types.h>

/**
 * SECTION:ipconfig
 * @title: IP configuration primitives
 * @short_description: Functions for IP configuration handling
 */

struct connman_ipconfig;

/*
 * TODO(sleffler) temporarily order 1-1 w/ element
 * type so these are interchangeable
 */
enum connman_ipconfig_type {
	CONNMAN_IPCONFIG_TYPE_PPP	= 6,
	CONNMAN_IPCONFIG_TYPE_IPV4	= 7,
	CONNMAN_IPCONFIG_TYPE_IPV6	= 8,
	CONNMAN_IPCONFIG_TYPE_DHCP	= 9,
	CONNMAN_IPCONFIG_TYPE_BOOTP	= 10,
	CONNMAN_IPCONFIG_TYPE_ZEROCONF	= 11,
};

struct connman_ipaddress {
	int mask;		/* which data have values */
#define	CONNMAN_IPCONFIG_AF	(1<<0)
#define	CONNMAN_IPCONFIG_MTU	(1<<1)
#define	CONNMAN_IPCONFIG_PREFIX	(1<<2)
#define	CONNMAN_IPCONFIG_LOCAL	(1<<3)
#define	CONNMAN_IPCONFIG_PEER	(1<<4)
#define	CONNMAN_IPCONFIG_BCAST	(1<<5)
#define	CONNMAN_IPCONFIG_GW	(1<<6)
#define	CONNMAN_IPCONFIG_DOMAIN	(1<<8)
#define	CONNMAN_IPCONFIG_DNS	(1<<9)
#define	CONNMAN_IPCONFIG_SEARCH	(1<<10)
	/*
	 * NB: mask is a superset of smask when the associated device
	 * is configured (i.e. has L3 state).  Otherwise mask is 0
	 * and smask indicates which (static) values have been set
	 * via the d-bus and/or profile.  See ipconfig_set_address
	 * and ipconfig_clear_address for the details.
	 */
	int smask;		/* static values from d-bus and/or profile */
	int af;			/* address family, e.g. AF_INET */
	int mtu;		/* maximum transmit unit in bytes */
	unsigned char prefixlen;/* prefix length, e.g. /8 for 255.255.255.0 */
	char *local;		/* str rep of local address */
	char *peer;		/* str rep of peer address for p2p link */
	char *broadcast;	/* str rep of broadcast address */
	char *gateway;		/* str rep of gateway address XXX (max 1) */
	char *domain_name;	/* DNS domain */
	char **dns_servers;	/* str rep of dns server addresses */
	char **search_domains;  /* domains to search for addresses */
};

#define	CONNMAN_IPCONFIG_ALL	0xffff

struct connman_ipconfig	*connman_ipconfig_create(enum connman_ipconfig_type,
			    const char *name, void *tag, int index,
			    connman_bool_t save_empty);
void			 connman_ipconfig_delete(struct connman_ipconfig *);
struct connman_ipconfig *connman_ipconfig_find(void *tag,
					       enum connman_ipconfig_type type);
struct connman_ipconfig	*connman_ipconfig_ref(struct connman_ipconfig *);
void			 connman_ipconfig_unref(struct connman_ipconfig *);

/* register/unregister within the object hierarchy */
struct connman_element;
int			connman_ipconfig_register(struct connman_ipconfig *,
			    struct connman_element *parent);
void			connman_ipconfig_unregister(struct connman_ipconfig *);
struct connman_device  *connman_ipconfig_get_device(struct connman_ipconfig *);

/* force state changes for all ipconfig's created w/ tag */
void			connman_ipconfig_request(void *tag);
void			connman_ipconfig_release(void *tag);
void			connman_ipconfig_renew(void *tag);
void			connman_ipconfig_set_error(struct connman_ipconfig *,
			    int);

const char		*connman_ipconfig_get_name(const struct
			    connman_ipconfig *);
const char		*connman_ipconfig_get_path(const struct
			    connman_ipconfig *);
int			connman_ipconfig_get_index(const struct
			    connman_ipconfig *ipconfig);
void 			connman_ipconfig_set_index(struct connman_ipconfig *,
						   int index);
const char		*connman_ipconfig_get_ifname(const struct
			    connman_ipconfig *);
void 			*connman_ipconfig_get_data(const struct connman_ipconfig *);
void			connman_ipconfig_set_data(struct connman_ipconfig *, void *);

void			connman_ipconfig_set_address(struct connman_ipconfig *,
			    const struct connman_ipaddress *ipaddr);
const struct connman_ipaddress *connman_ipconfig_get_ipaddress(
			    const struct connman_ipconfig *ipconfig);
void			connman_ipconfig_clear_address(struct connman_ipconfig *);
int			connman_ipconfig_add_route(struct connman_ipconfig *,
			     const char *addr, const char *netmask,
			     const char *gw);
void			connman_ipconfig_bind(struct connman_ipconfig *,
			    const struct connman_ipaddress *ipaddress);


#define CONNMAN_IPCONFIG_PRIORITY_LOW      -100
#define CONNMAN_IPCONFIG_PRIORITY_DEFAULT     0
#define CONNMAN_IPCONFIG_PRIORITY_HIGH      100

struct connman_ipconfig_driver {
	const char *name;
	enum connman_ipconfig_type type;
	int priority;
	int (*request)(struct connman_ipconfig *ipconfig);
	int (*release)(struct connman_ipconfig *ipconfig);
	int (*renew)(struct connman_ipconfig *ipconfig);
};

int			connman_ipconfig_driver_register(const struct
			    connman_ipconfig_driver *driver);
void			connman_ipconfig_driver_unregister(const struct
			    connman_ipconfig_driver *driver);

#ifdef __cplusplus
}
#endif

#endif /* __CONNMAN_IPCONFIG_H */
